//
//  AYSCollectVideoTableViewController.m
//  AiYingShiProject
//
//  Created by lanou on 15/11/5.
//  Copyright (c) 2015年 李晓理. All rights reserved.
//

#import "AYSCollectVideoTableViewController.h"
#import "AYSFavouriteHandle.h"
#import "AYSCategorySmallDeViewController.h"
@interface AYSCollectVideoTableViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;
// 当前的编辑模式
@property (nonatomic,assign) UITableViewCellEditingStyle editingStyle;

@end

@implementation AYSCollectVideoTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titleLabel.text = @"小视频";
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 65, SCREEN_WIDTH, SCREEN_HEIGHT - 65)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"video"];
    _tableView.allowsSelectionDuringEditing = YES;  // 编辑状态下可以选中
    [self.view addSubview:_tableView];
    
    UIButton *deleteButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [deleteButton setBackgroundImage:[UIImage imageNamed:@"edit"] forState:(UIControlStateNormal)];
    [deleteButton setBackgroundImage:[UIImage imageNamed:@"over"] forState:(UIControlStateSelected)];
    deleteButton.frame = CGRectMake(SCREEN_WIDTH - 80, 30, 30, 30);
    [deleteButton addTarget:self action:@selector(cilckDeteleButton:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.topView addSubview:deleteButton];
    
     [[AYSFavouriteHandle shareInstance] setupVideoDataSource];
    
}
- (void)cilckDeteleButton:(UIButton *)button
{
    button.selected = !button.selected;
    [self deleteMovie];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[AYSFavouriteHandle shareInstance]countOfVideo];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"video" forIndexPath:indexPath];
    AYSSmallListModel *movie = [[AYSFavouriteHandle shareInstance] videoForRow:indexPath.row];
    cell.textLabel.text = movie.title;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    AYSSmallListModel *movie = [[AYSFavouriteHandle shareInstance] videoForRow:indexPath.row];
    AYSCategorySmallDeViewController *playVC = [[AYSCategorySmallDeViewController alloc] init];
    playVC.model = movie;
    [self.navigationController pushViewController:playVC animated:YES];
}

// 让tableview变成可编辑状态
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [_tableView setEditing:editing animated:animated];
}

// 指定哪一行的编辑按钮状态指定编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return _editingStyle;
}

// 判断点击按钮的样式来作删除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        [[AYSFavouriteHandle shareInstance] deleteVideoForRow:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}

- (void)deleteMovie
{
    _editingStyle = UITableViewCellEditingStyleDelete;
    BOOL isEditing = self.tableView.isEditing;
    [self.tableView setEditing:!isEditing animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



/*
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#@"reuseIdentifier"#> forIndexPath:indexPath];
    
    // Configure the cell...
    
    return cell;
}
*/

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
